package test1.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import test1.entity.Account;
import test1.mapper.AccountMapper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author: mornd
 * @dateTime: 2023/5/15 - 20:33
 */
@Service
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Transactional(rollbackFor = Exception.class)
    public void transfer(int from, int to, double money) {
        try {
            Account account = accountMapper.getById(from);
            if(account != null && (account.getBalance() - money >= 0)) {
                accountMapper.update(from, -money);
                new FileInputStream("abc");
                accountMapper.update(to, money);
            }
        } catch (FileNotFoundException e) {
            //throw new RuntimeException(e);
            //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        }
    }
}
